package com.multicampus.simplewiki.control.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.multicampus.simplewiki.ctrl.LoginManager;
import com.multicampus.simplewiki.model.User;

public class LoginController extends SimpleFormController {
	LoginManager loginManager;
	
	public void setLoginManager(LoginManager loginManager) {
		this.loginManager = loginManager;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors) throws Exception {
		User user = (User)command;
		user = loginManager.validUser(user);
		
		if(user != null){
			request.getSession().setAttribute("user", user);
			return new ModelAndView("redirect:/view_page.do");			
		}
		else{
			return new ModelAndView("forward:/index.jsp");
		}
	}
}
